package com.yh.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.apache.log4j.Logger;

import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.UUID;

/**
 * 随机属性构建器
 *
 * @author yuhuan
 * @date 2022/01/13
 */
public class RandomAttributeBuilder {
    /**
     * 日志
     */
    private static final Logger logger = Logger.getLogger(RandomAttributeBuilder.class);

    /**
     * 随机源
     */
    private static int[] randomSource = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    /**
     * 利用UUID创建全局唯一的ID，不便于阅读
     */
    public static String createidfromUuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 利用随机数和日期产生一个纯数字的字符串ID，不适用于并发条件
     */
    public static String createIdFromRandomNumAndDate() {
        StringBuilder stringBuilder = new StringBuilder();
        DateTime nowTime = DateTime.now();
        // 将当前的年份添加到缓冲区（4位）
        stringBuilder.append(DateUtil.year(nowTime));
        // 将当前的月份添加到缓冲区（2位）
        int month = DateUtil.month(nowTime);
        stringBuilder.append(repairZero(month));
        // 将当前的日期添加到缓冲区（2位）
        int dayOfMonth = DateUtil.dayOfMonth(nowTime);
        stringBuilder.append(repairZero(dayOfMonth));
        // 将当前的小时数添加到缓冲区（2位）
        int hour = DateUtil.hour(nowTime, true);
        stringBuilder.append(repairZero(hour));
        // 将当前的分钟数添加到缓冲区（2位）
        int minute = DateUtil.minute(nowTime);
        stringBuilder.append(repairZero(minute));
        // 将当前的秒数添加到缓冲区（2位）
        int second = DateUtil.second(nowTime);
        stringBuilder.append(repairZero(second));
        // 将水机码添加到缓冲区
        stringBuilder.append(createRandomCode(2));
        return stringBuilder.toString();
    }

    /**
     * 补全零位
     */
    private static String repairZero(int num) {
        if (num < Constants.ZERO_BOUND) {
            return "0" + num;
        }
        return String.valueOf(num);
    }

    /**
     * 创建纯数字的随机码
     *
     * @randomCodeLen 随机码的长度
     */
    public static String createRandomCode(int randomCodeLen) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < randomCodeLen; i++) {
            // 原代码"int index = (int) (Math.random()*10)"
            Random random = new Random();
            int index = random.nextInt(10);
            stringBuilder.append(randomSource[index]);
        }
        return stringBuilder.toString();
    }

    /**
     * 生成随机的昵称字符串（中文-真随机）
     */
    public static String createRandomStr(int randomStrLen) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < randomStrLen; i++) {
            String str = null;
            // 定义高低位
            int hightPos, lowPos;
            Random random = new Random();
            // 获取高位值
            hightPos = (176 + Math.abs(random.nextInt(39)));
            // 获取低位值
            lowPos = (161 + Math.abs(random.nextInt(93)));
            byte[] b = new byte[2];
            b[0] = (new Integer(hightPos).byteValue());
            b[1] = (new Integer(lowPos).byteValue());
            try {
                // 转成中文
                str = new String(b, "GBK");
            } catch (UnsupportedEncodingException ex) {
                logger.error("<<<<< 生成随机字符时出现不支持的编码格式！");
            }
            stringBuilder.append(str);
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        String randomJianHan = createRandomStr(5);
        System.out.println(randomJianHan);
    }
}
